package cn.demoxy.service.impl;

import cn.demoxy.component.MinioUtils;
import cn.demoxy.constant.CommonConstant;
import cn.demoxy.enums.ExceptionMessageEnum;
import cn.demoxy.exception.DemoException;
import cn.demoxy.model.entity.DemoFile;
import cn.demoxy.mapper.DemoFileMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.StatObjectArgs;
import io.minio.StatObjectResponse;
import io.minio.errors.*;
import lombok.AllArgsConstructor;
import cn.hutool.core.io.IoUtil;
import org.springframework.stereotype.Service;
import cn.demoxy.service.IDemoFileService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

/**
 * 文件管理表 业务实现类
 *
 * @author demo
 * @date 2022-05-26
 */
@Service
@AllArgsConstructor
public class DemoFileServiceImpl extends ServiceImpl<DemoFileMapper, DemoFile> implements IDemoFileService {

    private MinioUtils minioUtils;

    @Override
    public void uploadFiles(MultipartFile[] files) {
        if (files != null && files.length == 1) {
            boolean isMax = minioUtils.checkFileSize(files[0].getSize(), CommonConstant.FILE_SIZE_MAX, "M");
            if (isMax) {
                //获取文件原名称
                String originalFilename = files[0].getOriginalFilename();
                //返回生成的UUID文件名
                String fileName = minioUtils.uploadOne(files[0]);
                //获取文件类型(后缀名)
                String suffixName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
                long size = files[0].getSize();
                DemoFile demoFile = new DemoFile().setOriginal(originalFilename).setType(suffixName).setFileSize(minioUtils.formatFileSize(size))
                        .setFileName(fileName);
                baseMapper.insert(demoFile);
            } else {
                //抛出文件超过限制异常
                throw new DemoException(ExceptionMessageEnum.FILE_SIZE_IS_MAX.getCode(), ExceptionMessageEnum.FILE_SIZE_IS_MAX.getMsg());
            }
        }
    }

    @Override
    public void downloadFile(String fileName, HttpServletResponse response) {
        String objectUrl = minioUtils.getObjectUrl(fileName);
        System.out.println(objectUrl);
        minioUtils.download(response, fileName);
    }

    @Override
    public String previewFile(String fileName) {
        String objectUrl = minioUtils.getObjectUrl(fileName);
        return objectUrl;
    }

    @Override
    public boolean removeFile(String id)  {
        String fileName = baseMapper.selectById(id).getFileName();
        try {
            minioUtils.removeObject(fileName);
            baseMapper.deleteById(id);
            return true;
        } catch (Exception e) {
            return false;
        }

    }

    @Override
    public String uploadFace(MultipartFile file) {
        if (file != null) {
            boolean isMax = minioUtils.checkFileSize(file.getSize(), CommonConstant.FILE_SIZE_MAX, "M");
            if (isMax) {
                //返回生成的UUID文件名
                String fileName = minioUtils.uploadOne(file);
                return fileName;
            } else {
                //抛出文件超过限制异常
                throw new DemoException(ExceptionMessageEnum.FILE_SIZE_IS_MAX.getCode(), ExceptionMessageEnum.FILE_SIZE_IS_MAX.getMsg());
            }
        }
        return null;
    }
}
